思路(from Discuss)
either up or down begin
繼承上一次(inc/dec)的數量,再加一
程式碼
class Solution:
def maxTurbulenceSize(self, arr: List[int]) -> int:
inc = 1
dec = 1
res = 1
for i in range(1,len(arr)):
if arr[i] > arr[i-1]:
inc = dec+1
dec = 1
elif arr[i] < arr[i-1]:
dec = inc+1
inc = 1
else:
inc = 1
dec = 1
print("i-inc-dec:",i,inc,dec)
#res = max(inc,dec)
res = max([inc,dec,res])
print("res:",res)
return res
Reference
每月挑戰(2021.09.22)
(Medium) LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters
題意
思路
&
|
程式碼
class Solution:
def maxLength(self, arr: List[str]) -> int:
A = [set()] # set() => {}
for a in arr:
if len(set(a))!=len(a):
continue
for element in A[:]:
if set(a)&element:
continue
A.append(set(a)|element)
print(A)
maxLen = (-1)
for _ in A:
if len(_)>maxLen:
maxLen = len(_)
return maxLen
目的刷
面試:
> Find Good Job!
Day 8: Recap Day [1-7] & Enhance